<?php
namespace Home\Controller;

class MatchController extends FuncController {

    function __construct(){
        parent::__construct();

        $this->mdlTeam = D('team');
        $this->mdlTeamPlayer = D('team_player');
        $this->mdlTeamData = D('team_data');

        $this->mdlPlayer = D('player');
        $this->mdlPlayerData = D('player_data');

        $this->mdlMatch = D('match');
        $this->mdlMatchTeam = D('match_team');
        $this->mdlMatchTeamPlayer = D('match_team_player');


        $this->player = $this->mdlPlayer->findPlayer($this->userid);
    }

    public function index(){
        // list
        $matchList = $this->mdlMatch->findMatchByPlayerId($this->player['player_id']);
        $this->assign('matchList',$matchList);

        $this->pDisplay( T('Match/index'), true );
    }

    public function create(){
        $teamPlayer = $this->mdlTeamPlayer->findTeamPlayerByPlayerId($this->player['player_id']);
        $teamPlayer['user_id'] = $this->userid;

        // away teams
        $awayTeams = $this->mdlTeam->findAwayTeams($teamPlayer['team_id']);

        $this->assign('teamPlayer',$teamPlayer);
        $this->assign('awayTeams',$awayTeams);

        $this->pDisplay( T('Match/create'), true );
    }

    public function createMatch(){
// $this->EE($_POST);

        // save match
        $rs = $this->mdlMatch->createMatch();
        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
            exit;
        }
        // save match team
        $match_id = $rs['dt']['match_id'];
        $rs = $this->mdlMatchTeam->createMatchTeam($match_id);
        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
            exit;
        }
        // save match team player
        $rs = $this->mdlMatchTeamPlayer->createMatchTeamPlayer($match_id);
        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
            exit;
        }

        // create team data
        $rs = $this->mdlTeamData->createTeamData($match_id);
        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
            exit;
        }


        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
        } else {
            $this->success($rs['msg'], './index.php?m=Home&c=Match&a=index', 2);
        }
    }

    public function join(){
// $this->EE($_GET);
        if (!isset($_GET['player_id'])) {
            $_GET['player_id'] = $this->player['player_id'];
        }

        $rs = $this->mdlMatchTeamPlayer->join($_GET);
        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
            exit;
        }

        // create player data
        $rs = $this->mdlPlayerData->createPlayerData($_GET);

        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
        } else {
            $this->success($rs['msg'], './index.php?m=Home&c=Match&a=index', 2);
        }
    }

    public function view(){
        $info = array();
        // match
        $info['match'] = $this->mdlMatch->findMatchByMatchId($_GET);
        // match team
        $info['team'] = $this->mdlTeam->findMatchTeam($info['match']);
        // match team data
        $info['teamData'] = $this->mdlTeamData->findMatchTeamData($info['match']);
        // assign
        $this->assign('info',$info);

        $this->pDisplay( T('Match/view'), true );
        exit;
    }

    public function show_match_team_players(){
        // match team player
        $players = $this->mdlMatchTeamPlayer->findMatchTeamPlayerList($_GET);
        // assign
        $this->assign('players',$players);

        $this->pDisplay( T('Match/show_match_team_players'), true );
        exit;



    }

    public function update_team_player_data(){
        // update match team player data
        $rs = $this->mdlPlayerData->updatePlayerData($_GET);
        // if (!$rs['status']){
        //     $this->error($rs['msg'], '', 2);
        //     exit;
        // }

        // update match team data
        $rs = $this->mdlTeamData->updateTeamData($_GET);

        if (!$rs['status']){
            $this->error($rs['msg'], '', 1);
        } else {
            $this->success($rs['msg'], "./index.php?m=Home&c=Match&a=view&match_id={$_GET['match_id']}&team_id={$_GET['team_id']}", 1);
        }
    }
}
